<?php
/**
 +------------------------------------------------------------------------------
 * KitOrder套件订单操作
 +------------------------------------------------------------------------------
 * @package  app
 * @author   alpha <QQ:411083658>
 * @date 2013-9-12
 * @time  下午13:38:07
 +------------------------------------------------------------------------------
 */
class KitOrder {
	protected $dao,$daogoods,$daolog,$post;
	protected $infoid=0;
	protected $debug=array();
	protected $isdebug = 0,$isecho = 0;
	/**
	 * 实例化分销商用户信息类函数
	 * */
	function __construct(){
		$this->dao = M('KitOrder');
		$this->daogoods = M('KitOrderGoods');
		$this->daolog = M('KitOrderLog');
	}
	/**
	 * 套件订单列表
	 * @param array     $_arr=array(
	 * 					'userid'=>用户ID,
	 * 					'status'=>订单状态,
	 * 					'pay_status'=>支付状态,
	 * 					'shipping_status'=>出货状态,
	 * 					'checkout_status'=>结算状态,
	 * 					'province' => 省ID,
	 * 					'city' => 市ID,
	 * 					'area' => 区ID
	 * 					)
	 * @return array
	 * */
	 function order_list($_arr=array(),$limit = '0,20',$order = 'id DESC'){
	 	$this->dao = M('KitOrder');
	 	$model = $this->dao;
		/*$where = '';
		if($_arr != 'all'){
			foreach ($_arr as $key => $value) {
				if(in_array($key, array('province','city','area'))){
					if($value > 0){
						if(empty($where)){
							$where = $key."='".$value."'";
						}else{
							$where .= " AND ".$key."='".$value."'";
						}
					}
				}else{
					if(empty($where)){
						$where = $key."='".$value."'";
					}else{
						$where .= " AND ".$key."='".$value."'";
					}
				}
			}
		}*/
		$where = array();
		if($_arr != 'all'){
			foreach ($_arr as $key => $value) {
				$where[$key] = $value;
			}
		}
		if (strtolower ( $limit ) == 'all') {
			return $model->where($where)->count ();
		}
		$res = $model->where($where)->order ( $order )->limit ( $limit )->select();
		return $res;
	 }
	 /**
	 * 获取订单信息
	 * @param int     $id 套件ID
	 * @param int     $userid 用户ID
	 * @return array
	 * */
	 function get_order_detail($id,$userid=0){
		$this->dao = M('KitOrder');
		if($userid != 0){
			return $this->select_ext('',array('userid'=>$userid),'*',false);
		}
		$ret = $this->select($id);
		return $ret;
	 }
	 /**
	 * 订单商品列表
	 * @param array     $_arr=array(
	 * 					'order_id'=>订单ID
	 * 					)
	 * @return array
	 * */
	 function order_goods_list($_arr=array(),$limit = '0,20',$order = 'id DESC',$isall = true){
		$model = M('KitOrderGoods');
		$where = '';
		if($_arr != 'all'){
			foreach ($_arr as $key => $value) {
				if(empty($where)){
					$where = $key."='".$value."'";
				}else{
					$where .= " AND ".$key."='".$value."'";
				}
			}
		}
		if($isall !== false){
			if (strtolower ( $limit ) == 'all') {
				return $model->where($where)->count ();
			}
			$rel = $model->where($where)->order ( $order )->limit ( $limit )->select();
			return $rel;
		}else{
			$rel = $model->where($where)->order ( $order )->select();
			return $rel;
		}
	 }
	 /**
	 * 订单日志列表
	 * @param array     $_arr=array(
	 * 					'order_id'=>订单ID
	 * 					)
	 * @return array
	 * */
	 function order_log_list($_arr=array(),$limit = '0,20',$order = 'id DESC',$isall = true){
		$model = M('KitOrderLog');
		$where = '';
		if($_arr != 'all'){
			foreach ($_arr as $key => $value) {
				if(empty($where)){
					$where = $key."='".$value."'";
				}else{
					$where .= " AND ".$key."='".$value."'";
				}
			}
		}
		if($isall !== false){
			if (strtolower ( $limit ) == 'all') {
				return $model->where($where)->count ();
			}
			$rel = $model->where($where)->order ( $order )->limit ( $limit )->select();
			return $rel;
		}else{
			$rel = $model->where($where)->order ( $order )->select();
			return $rel;
		}
	 }
	 /**
	  * 添加订单
	  * @param array    $info[i]=array(
	  * 					'type' => 产品类型,
	  * 					'old_price' => 原价格,
	  * 					'number' => 产品量,
	  * 					'price' => 商品售价
	  * 					)
	  * @param array 	$field_null		设置不可为空的字段
	  * @param int	 	$userid			订单用户编号
	  * @param int	 	$roleid			订单用户组编号
	  * @param int	 	$action_userid	操作人编号
	  * @param varchar 	$remark			操作特殊备注
	  * @param array    $region=array(
	  * 					'province' => 省ID,
	  * 					'city' => 市ID,
	  * 					'area' => 区ID
	  * 					)
	  * @param array 	成功返回 {"status":"1","msg":"信息","data":data,"id":序列号} 失败返回 {"status":"0","msg":"信息","data":data}
	  * */
	function order_add($info=array(),$field_null=array(),$userid=0,$roleid=0,$action_userid=0,$remark=null,$region=array()){
		$this->dao = M('KitOrder');
		if(intval($userid)<=0||intval($action_userid)<=0){//||intval($roleid)<=0
			return $this->_err("购买套件用户有误!");//用户和用户组有误
		}
		foreach ($field_null as $key => $value) {
			foreach ($info as $keys => $values) {
				if(empty($info[$keys][$key])){
					return $this->_err("第". ($keys+1) ."个商品的<<".$value.">>不能为空");
				}
			}
		}
		unset($this->post);
		$this->post['userid'] = $userid;
		$this->post['add_time'] = time ();
		$this->post['add_ip'] = get_client_ip();
		$province = $region['province']?$region['province']:0;
		$this->post['province'] = $province;
		$city = $region['city']?$region['city']:0;
		$this->post['city'] = $city;
		$area = $region['area']?$region['area']:0;
		$this->post['area'] = $area;
		$resid = $this->insert();
		if(false !== $resid){
			$count_all = 0;
			$amount_all = 0;
			$old_amount_all = 0;
			foreach ($info as $key => $value) {
				$goods['order_id'] = $resid;
				//设置商品类型
				$goods['type'] = $value['type'];
				//原价格
				$goods['old_price'] = $value['old_price']*100;
				//产品量
				$goods['number'] = $value['number'];
				$count_all +=$goods['number'];
				//单价
				$goods['price'] = $value['price']*100;
				//小计
				$goods['priceall'] = intval($goods['price']) * intval($goods['number']);
				$goods['oldpriceall'] = intval($goods['old_price']) * intval($goods['number']);
				$amount_all +=$goods['priceall'];
				$old_amount_all +=$goods['oldpriceall'];
				$this->insert_ext($this->daogoods,$goods);
			}
			$sn = '10' . date ( 'ymdH' ) . $resid;
			$order = array();
			$order['sn'] = $sn;
			$order['old_amount'] = $old_amount_all;
			$order['amount'] = $amount_all;
			$order['count_num'] = $count_all;
			$this->update_ext(M('KitOrder'),$order, $resid);
			$log['order_id'] = $resid;
			$log['user_id'] = $userid;
			$log['action_userid'] = $action_userid;
			$log['ctime'] = time();
			$log['status'] = 1;
			if($remark != null){
				$log['remark'] = $remark;
			}
			if(in_array($value['role_id'], array(13,12,10,22))){
				$is_user_role = $value['role_id'];
			}
			switch ($roleid) {
				case 13:
					$log['type'] = 4;
					//设置结束受理时间
					$log['accept_end_time'] = strtotime("+24 hours", $log['ctime']);;
					break;
				case 12:
					$log['type'] = 3;
					//设置结束受理时间
					$log['accept_end_time'] = strtotime("+24 hours", $log['ctime']);;
					break;
				case 10:
					$log['type'] = 2;
					//设置结束受理时间
					$log['accept_end_time'] = strtotime("+24 hours", $log['ctime']);;
					break;
				case 22:
					$log['type'] = 1;
					//设置结束受理时间
					$log['accept_end_time'] = strtotime("+24 hours", $log['ctime']);;
					break;
				default:
					$log['type'] = 0;
					//设置结束受理时间
					$log['accept_end_time'] = 0;
					break;
			}
			$this->insert_ext($this->daolog,$log);
			$res=array('id'=>$resid);
			return $this->_suc($res,'下单成功');
		}else{
			return $this->_err("下单失败请重试");
		}
	}
	/**
	 * 修改订单
	 * @param array     $info=array(
	 * 					'id' => 订单ID,
	 * 					...
	 * 					)
	 * @param int	 	$userid			用户编号
	 * @param int	 	$roleid			用户组编号
	 * @param int	 	$action_userid	操作人编号
	 * @param varchar 	$remark			操作特殊备注
	 * @return array 	成功返回 {"status":"1","msg":"信息","data":data,"mStatus":是否执行修改} 失败返回 {"status":"0","msg":"信息","data":data}
	 * */
	function order_edit($info=array(),$userid=0,$roleid=0,$action_userid=0,$remark=null){
		$this->dao = M('KitOrder');
		$this->infoid = $info['id'];
		if(empty($this->infoid)){
			return $this->_err("没有要修改的数据或数据ID不可为空");
		}
		unset($this->post);
		$this->post['id'] = $this->infoid;
		$this->post=$info;
		$ret = $this->update();
		if(false !== $ret){
			$log['order_id'] = $this->infoid;
			$log['user_id'] = $userid;
			$log['action_userid'] = $action_userid;
			$log['ctime'] = time();
			$log['status'] = 1;
			if($remark != null){
				$log['remark'] = $remark;
			}
			$log['type'] = 8;
			//设置结束受理时间
			$log['accept_end_time'] = 0;
			$this->insert_ext($this->daolog,$log);
			$res=array('mStatus'=>$status);
			return $this->_suc($res,'修改成功');
		}else{
			return $this->_err("支付失败请重试");
		}
	}
	/**
	 * 更新支付
	 * @param array     $info=array(
	 * 					'id' => 订单ID
	 * 					)
	 * @param int	 	$userid			用户编号
	 * @param int	 	$roleid			用户组编号
	 * @param int	 	$action_userid	操作人编号
	 * @param varchar 	$remark			操作特殊备注
	 * @param array 	成功返回 {"status":"1","msg":"信息","data":data,"mStatus":是否执行修改} 失败返回 {"status":"0","msg":"信息","data":data}
	 * */
	function order_pay_status($info=array(),$userid=0,$roleid=0,$action_userid=0,$remark=null){
		$this->dao = M('KitOrder');
		$this->infoid = $info['id'];
		if(empty($this->infoid)){
			return $this->_err("没有要修改的数据或数据ID不可为空");
		}
		unset($this->post);
		$this->post['id'] = $this->infoid;
		$this->post['pay_status'] = 1;
		$this->post['pay_time'] = time();
		$ret = $this->update();
		if(false !== $ret){
			$log['order_id'] = $this->infoid;
			$log['user_id'] = $userid;
			$log['action_userid'] = $action_userid;
			$log['ctime'] = time();
			$log['status'] = 1;
			if($remark != null){
				$log['remark'] = $remark;
			}
			$log['type'] = 9;
			//设置结束受理时间
			$log['accept_end_time'] = 0;
			$this->insert_ext($this->daolog,$log);
			$res=array('mStatus'=>$status);
			return $this->_suc($res,'支付成功');
		}else{
			return $this->_err("支付失败请重试");
		}
	}
	/**
	 * 更新出货
	 * @param array     $info=array(
	 * 					'id' => 订单ID
	 * 					)
	 * @param int	 	$userid			用户编号
	 * @param int	 	$roleid			用户组编号
	 * @param int	 	$action_userid	操作人编号
	 * @param varchar 	$remark			操作特殊备注
	 * @param array 	成功返回 {"status":"1","msg":"信息","data":data,"mStatus":是否执行修改} 失败返回 {"status":"0","msg":"信息","data":data}
	 * */
	function order_shipping_status($info=array(),$userid=0,$roleid=0,$action_userid=0,$remark=null){
		$this->dao = M('KitOrder');
		$this->infoid = $info['id'];
		if(empty($this->infoid)){
			return $this->_err("没有要修改的数据或数据ID不可为空");
		}
		unset($this->post);
		$this->post['id'] = $this->infoid;
		$this->post['shipping_status'] = 1;
		$this->post['shipping_time'] = time();
		$ret = $this->update();
		if(false !== $ret){
			$log['order_id'] = $this->infoid;
			$log['user_id'] = $userid;
			$log['action_userid'] = $action_userid;
			$log['ctime'] = time();
			$log['status'] = 1;
			if($remark != null){
				$log['remark'] = $remark;
			}
			$log['type'] = 10;
			//设置结束受理时间
			$log['accept_end_time'] = 0;
			$this->insert_ext($this->daolog,$log);
			$res=array('mStatus'=>$status);
			return $this->_suc($res,'出货成功');
		}else{
			return $this->_err("出货失败请重试");
		}
	} 
	/**
	 * 更新结算
	 * @param array     $info=array(
	 * 					'id' => 订单ID
	 * 					)
	 * @param int	 	$userid			用户编号
	 * @param int	 	$roleid			用户组编号
	 * @param int	 	$action_userid	操作人编号
	 * @param varchar 	$remark			操作特殊备注
	 * @param array 	成功返回 {"status":"1","msg":"信息","data":data,"mStatus":是否执行修改} 失败返回 {"status":"0","msg":"信息","data":data}
	 * */
	function order_checkout_status($info=array(),$userid=0,$roleid=0,$action_userid=0,$remark=null){
		$this->dao = M('KitOrder');
		$this->infoid = $info['id'];
		if(empty($this->infoid)){
			return $this->_err("没有要修改的数据或数据ID不可为空");
		}
		unset($this->post);
		$this->post['id'] = $this->infoid;
		$this->post['checkout_status'] = 1;
		$this->post['checkout_time'] = time();
		$ret = $this->update();
		if(false !== $ret){
			$log['order_id'] = $this->infoid;
			$log['user_id'] = $userid;
			$log['action_userid'] = $action_userid;
			$log['ctime'] = time();
			$log['status'] = 1;
			if($remark != null){
				$log['remark'] = $remark;
			}
			$log['type'] = 11;
			//设置结束受理时间
			$log['accept_end_time'] = 0;
			$this->insert_ext($this->daolog,$log);
			$res=array('mStatus'=>$status);
			return $this->_suc($res,'结算成功');
		}else{
			return $this->_err("结算失败请重试");
		}
	}
	/**
	 * 更新状态
	 * @param array     $info=array(
	 * 					'id' => 订单ID
	 * 					)
	 * @param int	 	$userid			用户编号
	 * @param int	 	$roleid			用户组编号
	 * @param int	 	$action_userid	操作人编号
	 * @param varchar 	$remark			操作特殊备注
	 * @param array 	成功返回 {"status":"1","msg":"信息","data":data,"mStatus":是否执行修改} 失败返回 {"status":"0","msg":"信息","data":data}
	 * */
	function order_status($info=array(),$userid=0,$roleid=0,$action_userid=0,$remark=null){
		$this->dao = M('KitOrder');
		$this->infoid = $info['id'];
		if(empty($this->infoid)){
			return $this->_err("没有要修改的数据或数据ID不可为空");
		}
		unset($this->post);
		$this->post['id'] = $this->infoid;
		$this->post['status'] = 6;
		$ret = $this->update();
		if(false !== $ret){
			$log['order_id'] = $this->infoid;
			$log['user_id'] = $userid;
			$log['action_userid'] = $action_userid;
			$log['ctime'] = time();
			$log['status'] = 1;
			if($remark != null){
				$log['remark'] = $remark;
			}
			$log['type'] = 6;
			//设置结束受理时间
			$log['accept_end_time'] = 0;
			$this->insert_ext($this->daolog,$log);
			$res=array('mStatus'=>$status);
			return $this->_suc($res,'订单完成');
		}else{
			return $this->_err("完成订单失败请重试");
		}
	}
	/**
	 * 计算受理时间
	 * @param array     $info=array(
	 * 					'type' => 用户类型,2，3，4，5
	 * 					'start_time' => 开始时间,
	 * 					't_length' => 时长
	 * 					)
	 * @param array 	$field_null		设置不可为空的字段
	 * @return string	成功返回 {"status":"1","msg":"信息","data":data,"time":最终时间} 失败返回 {"status":"0","msg":"信息","data":data}
	 * */
	function count_time($info=array(),$field_null=array()){
		foreach ($field_null as $key => $value) {
			if(empty($info[$key])){
				return $this->_err($value."不能为空");
			}
		}
		if (!eregi($info['type'],"2345")) {
			return $this->_err("不可受理,可能没有权限");
		}
		$res=self::start_end_time($info);
		if($res['now_time'] < $res['end_time']  && $res['now_time'] > $res['start_time']){
			return $this->_suc($res,'可以受理');
		}
		//dump(($res));exit;
		return $this->_err("不可受理,可能不在受理期限内");
	}
	function start_end_time($info=array()){
		$start_time = 0;
		$end_time = 0;
		if(empty($info['t_length'])){
			switch (intval($info['type'])){
				case 2 :
					$start_time = $info['start_time'];
					$end_time = strtotime("+24 hours", $info['start_time']);
					break;
				case 3 :
					$end_time = strtotime("+48 hours", $info['start_time']);
					$start_time = strtotime("-24 hours", $end_time);
					break;
				case 4 :
					$end_time = strtotime("+72 hours", $info['start_time']);
					$start_time = strtotime("-24 hours", $end_time);
					break;
				case 5 :
					$end_time = strtotime("+84 hours", $info['start_time']);
					$start_time = strtotime("-12 hours", $end_time);
					break;
			}
		}else{
			$start_time = $info['start_time'];
			$end_time = strtotime("+".intval($info['t_length'])." hours", $info['start_time']);
		}
		$now_time = time();
		$res = array('now_time'=>$now_time,'start_time'=>$start_time,'end_time'=>$end_time);
		return $res;
	}
	/**
	 * 分销商受理订单
	 * @param array     $info=array(
	 * 					'start_time' => 开始时间,
	 * 					'id' => 订单ID
	 * 					)
	 * @param int	 	$userid			用户编号
	 * @param int	 	$roleid			用户组编号
	 * @param int	 	$action_userid	操作人编号
	 * @param varchar 	$remark			操作特殊备注
	 * @param array 	成功返回 {"status":"1","msg":"信息","data":data,"mStatus":是否执行修改} 失败返回 {"status":"0","msg":"信息","data":data}
	 * */
	function accept_order($info=array(),$userid=0,$roleid=0,$action_userid=0,$remark=null,$accept_time=0){
		$this->infoid = $info['id'];
		if(empty($this->infoid)){
			return $this->_err("没有要修改的数据或数据ID不可为空");
		}
		$user_level = 0;
		$user_superiors = 0;
		$ct = array();
		$arr = array();
		$arr_superiors = array(2,3,4,5);
		switch (intval($roleid)){
			case 10 :
				$user_level = 2;
				break;
			case 12 :
				$user_superiors = 2;
				$user_level = 3;
				break;
			case 13 :
				$user_superiors = 3;
				$user_level = 4;
				break;
			case 14 :
				$user_superiors = 4;
				$user_level = 5;
				break;
		}
		if($user_level == 0){
			return $this->_err("不可受理,可能没有权限");
		}
		if($user_superiors==0){
			$res = $this->select_ext($this->daolog,array('order_id'=>$this->infoid,'type'=>$user_level),'status,accept_end_time',false);
			if(false !== $res){
				return $this->_err("请不要重复受理!");
			}else{
				$ct = $this->count_time(array('type'=>$user_level,'start_time'=>$info['start_time']));
				$arr=object_array(json_decode($ct));
				if($arr['status'] == 0){
					return $ct;
				}
			}
		}else{
			$ret = $this->select_ext($this->daolog,array('order_id'=>$this->infoid,'type'=>$user_superiors),'status,accept_end_time',false);
			//dump($ret);exit;
			if(false !== $ret){
				$res = $this->select_ext($this->daolog,array('order_id'=>$this->infoid,'type'=>$user_level),'status,accept_end_time',false);
				//dump($res);exit;
				if(false !== $res){
					return $this->_err("请不要重复受理!");
				}else{
					$ct = $this->start_end_time(array('type'=>$user_level,'start_time'=>$info['start_time']));
					$arr=$ct;
					/*dump(date('Y-m-d H:m:s',$arr['now_time']));
					dump(date('Y-m-d H:m:s',$arr['start_time']));
					dump(date('Y-m-d H:m:s',$arr['end_time']));
					dump($arr);exit;*/
					if($arr['now_time'] < $arr['end_time']){
						$this->dao = M('KitOrder');
						unset($this->post);
						$this->post['id'] = $this->infoid;
						$this->post['status'] = $user_level;
						$status = $this->update();
						if(false !== $status){
							$log['order_id'] = $this->infoid;
							$log['user_id'] = $userid;
							$log['action_userid'] = $action_userid;
							$log['ctime'] = time();
							$log['status'] = 1;
							if($remark != null){
								$log['remark'] = $remark;
							}
							$log['type'] = $user_level;
							//设置结束受理时间
							$log['accept_end_time'] = $arr['end_time'];
							$this->insert_ext($this->daolog,$log);
							$res=array('mStatus'=>$status);
							return $this->_suc($res,'受理订单成功');
						}else{
							return $this->_err("受理订单失败请重试");
						}
					}else{
						$ct = $this->count_time(array('type'=>$user_level,'start_time'=>$info['start_time']));
						$arr=object_array(json_decode($ct));
						if($arr['status'] == 0){
							return $ct;
						}
					}
				}
			}else{
				foreach ($arr_superiors as $key => $value) {
					$ret = $this->select_ext($this->daolog,array('order_id'=>$this->infoid,'type'=>$value),'accept_end_time',false);
					//dump($ret);exit;
					//dump($value .'<'. $user_level);
					if($value < $user_level){
						if(false !== $ret){
							
						}else{
							if(empty($accept_time)){
								$ct = $this->start_end_time(array('type'=>$value,'start_time'=>$info['start_time']));
								//dump($ct);
								//dump(date('Y-m-d H:m:s',$ct['now_time']));dump(date('Y-m-d H:m:s',$ct['start_time']));dump(date('Y-m-d H:m:s',$ct['end_time']));exit;
								$arr=$ct;
								if($arr['now_time'] > $arr['end_time']){
									if($arr['status'] == 0){
										$insert_info['order_id'] = $this->infoid;
										$insert_info['user_id'] = $userid;
										$insert_info['action_userid'] = 0;
										$insert_info['ctime'] = time();
										$insert_info['status'] = 0;
										$insert_info['type'] = $value;
										$insert_info['accept_end_time'] = $arr['end_time'];
										$this->info_add($insert_info);
									}
								}
							}else{
								$insert_info['order_id'] = $this->infoid;
								$insert_info['user_id'] = $userid;
								$insert_info['action_userid'] = 0;
								$insert_info['ctime'] = time();
								$insert_info['status'] = 0;
								$insert_info['type'] = $value;
								$insert_info['remark'] = $remark;
								$remark = '';
								$insert_info['accept_end_time'] = $accept_time;
								$accept_time = 0;
								$this->info_add($insert_info);
							}
						}
					}else if($value == $user_level){
						if(false !== $ret){
							return $this->_err("请不要重复受理!");
						}else{
							$ct = $this->count_time(array('type'=>$user_level,'start_time'=>$info['start_time']));
							//dump($ct);exit;
							$arr=object_array(json_decode($ct));
							if($arr['status'] == 0){
								return $ct;
							}
						}
					}
				}
			}
		}
		if($arr['now_time'] > $arr['start_time'] && $arr['now_time'] < $arr['end_time']){
			$this->dao = M('KitOrder');
			unset($this->post);
			$this->post['id'] = $this->infoid;
			$this->post['status'] = $user_level;
			$ret = $this->update();
			if(false !== $ret){
				$log['order_id'] = $this->infoid;
				$log['user_id'] = $userid;
				$log['action_userid'] = $action_userid;
				$log['ctime'] = time();
				$log['status'] = 1;
				if($remark != null){
					$log['remark'] = $remark;
				}
				$log['type'] = $user_level;
				//设置结束受理时间
				$log['accept_end_time'] = $arr['end_time'];
				$this->insert_ext($this->daolog,$log);
				$res=array('mStatus'=>$status);
				return $this->_suc($res,'受理订单成功');
			}else{
				return $this->_err("受理订单失败请重试");
			}
		}
		return $this->_err("受理失败,可能不在受理期限内或没有权限!");
	}
	/**
	 * 添加信息
	 * @param array $user 设置添加信息
	 * @return 成功返回  失败返回 false
	 * */
	function info_add($info=array()){
		$this->post = $info;
		$resid = $this->insert();
		if(false !== $resid){
			return $resid;
		}else{
			return false;
		}
	}
	/**
	 * 校验用户名是否合法
	 * @param array     $info=array(
	 * 					'id' => 订单ID
	 * 					)
	 * @param int	 	$userid			用户编号
	 * @param int	 	$roleid			用户组编号
	 * @param int	 	$action_userid	操作人编号
	 * @param varchar 	$remark			操作特殊备注
	 * @param array 	成功返回 {"status":"1","msg":"信息","data":data,"mStatus":是否执行修改} 失败返回 {"status":"0","msg":"信息","data":data}
	 * */
	private function _check($fn,$val){
		$model = $this->dao;
        $rel = $model->where($fn."='".$val."'")->find();
        if($rel !== false || !empty($rel)){
            return $rel;
        }else{
            return false;
        }
	}
	/**
     * 查询某个字段是否存在
	 * @param $fn 表字段名
	 * @param $val 值
     * @return 如果存在返回false，不存在返回true
     */
    private function field_exist($fn,$val){
        $model = $this->dao;
        $rel = $model->where($fn."='".$val."'")->find();
		//$this->debug = $model->getlastsql();
        if($rel === false || empty($rel) || $rel['id'] == $this->infoid){
            return true;
        }else{
            return false;
        }
    }
    /**
	 *请求接口类型异常
	 *@date 2012-05-06
	 *@time 上午08:10:19
	 */
	private function _err($str="暂无相关信息") {
		if($this->isecho == 1){
			//请求接口类型异常
			$res = array("status"=>0,"msg"=>$str,'data'=>$this->debug,'err_code'=>$this->err_code);
			if($this->isdebug==1){
				echo '<br>输出的数据:<br><textarea rows="15" cols="120">';
			}
			echo $this->myjson($res);
			if($this->isdebug==1){
				echo '</textarea>';
			}
			die();
		}else{
			$res = array("status"=>0,"msg"=>$str,'data'=>$this->debug,'err_code'=>$this->err_code);
			return $this->myjson($res);
		}
		
	}//end _err
	
	/**
	 *请求成功返回
	 *@date 2012-05-06
	 *@time 上午08:10:19
	 */
	private function _suc($arr=array(),$str="操作成功") {
		if($this->isecho == 1){
			//请求接口类型异常
			$res = array("status"=>1,"msg"=>$str,'data'=>$this->debug);
			if(!empty($arr)){
				$res=array_merge($res,$arr);
			}
			if($this->isdebug==1){
				echo '<br>输出的数据:<br><textarea rows="15" cols="120">';
			}
			echo $this->myjson($res);
			if($this->isdebug==1){
				echo '</textarea>';
			}
			die();
		}else{
			$res = array("status"=>1,"msg"=>$str,'data'=>$this->debug);
			if(!empty($arr)){
				$res=array_merge($res,$arr);
			}
			return $this->myjson($res);
		}
		
	}//end _suc
	/**
	 * 对url或其它字符转义
	 * */
	private function myjson($code) {
		$code = json_encode ( $this->urlencodeAry ( $code ) );
		return urldecode ( $code );
		//return $code;
	}
	private function urlencodeAry($data) {
		if (is_array ( $data )) {
			foreach ( $data as $key => $val ) {
				$data [$key] = $this->urlencodeAry ( $val );
			}
			return $data;
		} else {
			return urlencode ( $data );
		}
	}
	
	/**
	 * 操作插入数据表
	 * */
	private function insert(){
		$model = $this->dao;
		$_POST = $this->post;
		if (false === $model->create ()) {
			return false;
		}
		$result = $model->add ();
		//$this->debug = $model->getlastsql();
		if ($result !== false) {
			return $result;
		} else {
			return false;
		}
	}
	/**
	 * 操作更新数据表
	 * */
	private function update(){
		$model = $this->dao;
		if (false === $model->create ($this->post)) {
			return false;
		}
		$result = $model->save ();
		//$this->debug = $model->getlastsql();
		if (false !== $result) {
			return $result;
		} else {
			return false;
		}
	}
	/**
	 * 操作删除数据表
	 * */
	private function delete(){
		$model = $this->dao;
		$result = $model->delete ($this->infoid);
		if (false !== $result) {
			return $result;
		} else {
			return false;
		}
	}
	/**
	 * 操作查询数据表
	 * @param $id 查询用户ID
	 * @return 如果存在返回用户信息，不存在返回false，参数$id空返回所有用户信息
	 * */
	private function select($id){
		$model = $this->dao;
		if(!empty($id)){
			$result = $model->find ($id);
			return $result;
		}else{
			$result = $model->select ();
			return $result;
		}
	}
	/**
	 * 操作添加用户关联扩展或附属数据表
	 * @param $table_ext 自定义表名
	 * @param $field_value 自定义插入字段和数值
     * @return 如果成功返回相关信息，失败在返回false
	 * */
	private function insert_ext($table_ext,$field_value = array()){
		if(!empty($table_ext)){
			$this->dao = $table_ext;
		}
		$this->post = $field_value;
		$rel = $this->insert();
		return $rel;
	}
	/**
	 * 操作更新用户关联扩展或附属数据表
	 * @param $table_ext 自定义表名
	 * @param $field_value 自定义插入字段和数值
     * @return 如果成功返回相关信息，失败在返回false
	 * */
	private function update_ext($table_ext,$field_value = array(),$id){
		if(!empty($table_ext)){
			$this->dao = $table_ext;
		}
		if(!empty($id)){
			$field_value['id'] = $id;
			$this->post = $field_value;
		}
		$rel = $this->update();
		return $rel;
	}
	/**
	 * 操作删除用户关联扩展或附属数据表
     * @return 如果成功返回相关信息，失败在返回false
	 * */
	private function delete_ext($id){
		if(!empty($id)){
			$this->infoid = $id;
		}
		$rel = $this->delete();
	}
	/**
	 * 操作查询用户关联扩展或附属数据表
	 * @param $table_ext 自定义表名
	 * @param $field_value 自定义查询条件
	 * @param $field 自定义数据字段
	 * @param $isall 是否全部查询  是：true 否：false
     * @return 如果存在返回查询信息，不存在返回false
	 * */
	private function select_ext($table_ext,$field_value = array(),$field = "*",$isall = true){
		if(!empty($table_ext)){
			$this->dao = $table_ext;
		}
		$rel = $this->select_all($field_value,$field,$isall);
		return $rel;
	}
	/**
     * 自定义查询条件查询数据
	 * @param array $field_value 自定义查询条件
	 * @param $field 自定义数据字段
	 * @param $isall 是否全部查询  是：true 否：false
     * @return 如果存在返回所有信息，不存在返回false
     */   
    private function select_all($field_value = array(),$field = "*",$isall = true){
        $model = $this->dao;
		$where = "";
		foreach ($field_value as $key => $value) {
			if(empty($where)){
				$where = $key."='".$value."'";
			}else{
				$where .= " AND ".$key."='".$value."'";
			}
		}
		$rel = array();
		if($isall == true){
			$rel = $model->field($field)->where($where)->select();
			//$this->debug = $model->getlastsql();
		}else{
			$rel = $model->field($field)->where($where)->find();
			//$this->debug = $model->getlastsql();
		}
        if(!empty($rel)){//$rel !== false ||
            return $rel;
        }else{
            return false;
        }
    }
	/**
     * 查询用户是否存在
	 * @param $fn 表字段名
	 * @param $val 值
     * @return 如果存在返回一条用户信息，不存在返回false
     */
    private function select_field($fn,$val){
        $model = $this->dao;
        $rel = $model->where($fn."='".$val."'")->find();
        if($rel !== false || !empty($rel)){
            return $rel;
        }else{
            return false;
        }
    }
}